from fastapi import Request, Header


def get_cli_tenant(request: Request):
    return request.headers.get("X-TENANT-ID")


async def get_cli_user(request: Request):
    from common.g.g import G_REDIS
    from common.utils.depends.common import get_mgw
    from service.client_user_service import ClientUserService

    user_id = request.state.user_id
    tenant_id = request.state.tenant_id
    mgw = get_mgw(request)
    cus = ClientUserService(mgw, G_REDIS)
    ok, data_or_code = await cus.get_user_by_id(tenant_id, user_id)
    if ok:
        return data_or_code.data
    return data_or_code


def get_cli_user_id(request: Request):
    if hasattr(request.state, "user_id"):
        return request.state.user_id
    return None


def get_cli_tenant_id(request: Request):
    if hasattr(request.state, "tenant_id"):
        return request.state.tenant_id
    return None